<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\CarRecord;
use App\Models\Dispatcher;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class CarRecordController extends Controller
{
    /**
     * 获取用车记录列表
     */
    public function index(Request $request)
    {
        $openid = $request->input('openid');
        
        if (!$openid) {
            return response()->json([
                'code' => 400,
                'message' => '缺少openid参数'
            ]);
        }
        
        $records = CarRecord::where('openid', $openid)
            ->with('dispatcher')
            ->orderBy('created_at', 'desc')
            ->get();
            
        return response()->json([
            'code' => 200,
            'message' => '获取成功',
            'data' => $records
        ]);
    }
    
    /**
     * 创建用车记录
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'openid' => 'required|string',
            'use_date' => 'required|date',
            'car_type' => 'required|in:daily_rent,fixed_price',
            'car_model' => 'required|string',
            'driver_name' => 'required|string',
            'license_plate' => 'required|string',
            'dispatcher_id' => 'required|exists:dispatchers,id',
            'passenger_name' => 'required|string',
            'start_time' => 'required',
            'end_time' => 'required',
        ]);
        
        if ($validator->fails()) {
            return response()->json([
                'code' => 400,
                'message' => '参数验证失败',
                'errors' => $validator->errors()
            ]);
        }
        
        $data = $request->all();
        $record = CarRecord::create($data);
        
        // 计算总金额
        $record->total_amount = $record->calculateTotal();
        $record->save();
        
        return response()->json([
            'code' => 200,
            'message' => '创建成功',
            'data' => $record->load('dispatcher')
        ]);
    }
    
    /**
     * 获取单个用车记录
     */
    public function show($id)
    {
        $record = CarRecord::with('dispatcher')->find($id);
        
        if (!$record) {
            return response()->json([
                'code' => 404,
                'message' => '记录不存在'
            ]);
        }
        
        return response()->json([
            'code' => 200,
            'message' => '获取成功',
            'data' => $record
        ]);
    }
    
    /**
     * 更新用车记录
     */
    public function update(Request $request, $id)
    {
        $record = CarRecord::find($id);
        
        if (!$record) {
            return response()->json([
                'code' => 404,
                'message' => '记录不存在'
            ]);
        }
        
        $validator = Validator::make($request->all(), [
            'use_date' => 'date',
            'car_type' => 'in:daily_rent,fixed_price',
            'dispatcher_id' => 'exists:dispatchers,id',
        ]);
        
        if ($validator->fails()) {
            return response()->json([
                'code' => 400,
                'message' => '参数验证失败',
                'errors' => $validator->errors()
            ]);
        }
        
        $record->update($request->all());
        
        // 重新计算总金额
        $record->total_amount = $record->calculateTotal();
        $record->save();
        
        return response()->json([
            'code' => 200,
            'message' => '更新成功',
            'data' => $record->load('dispatcher')
        ]);
    }
    
    /**
     * 删除用车记录
     */
    public function destroy($id)
    {
        $record = CarRecord::find($id);
        
        if (!$record) {
            return response()->json([
                'code' => 404,
                'message' => '记录不存在'
            ]);
        }
        
        $record->delete();
        
        return response()->json([
            'code' => 200,
            'message' => '删除成功'
        ]);
    }
    
    /**
     * 获取调车人列表
     */
    public function getDispatchers()
    {
        $dispatchers = Dispatcher::where('status', 1)->get();
        
        return response()->json([
            'code' => 200,
            'message' => '获取成功',
            'data' => $dispatchers
        ]);
    }
}
